<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>文章表单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">文章标题</label>
        <div class="layui-input-inline">
            <input type="text" name="title" id="title" lay-verify="required" placeholder="请输入文章标题" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章头图</label>
        <div class="layui-input-inline">
            <input name="headImgUrl" lay-verify="required" id="LAY_headImgSrc" placeholder=""
                   autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline layui-btn-container" style="width: auto;">
            <button type="button" class="layui-btn layui-btn-primary" id="LAY_headImgUpload">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <button class="layui-btn layui-btn-primary" layadmin-event="headImgPreview">查看图片
            </button>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章内容</label>
        <div class="layui-input-inline" style="width: 80%; ">
            <textarea id="content" name="content" style="display: none;"
                      class="layui-textarea" lay-verify="content"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">发布状态</label>
        <div class="layui-input-inline">
            <input type="checkbox" id="pushFlag" lay-filter="status" name="pushFlag"
                   lay-skin="switch"
                   lay-text="已发布|待修改">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit"
               value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit"
               value="确认编辑">
    </div>
</div>


<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'set', 'layedit'], function () {
        var $ = layui.$
            , form = layui.form
            , layedit = layui.layedit;


        // layedit set
        layedit.set({
            uploadImage: {
                url: '/file/upload'
                , type: 'post'
            }
            , height: 900
        });

        //富文本编辑器
        var id = layedit.build('content');

        form.verify({
            content: function (value) {
                return layedit.sync(id);
            }
        });

        //监听表单提交
        form.on('submit(layuiadmin-app-form-submit)', function (data) {
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

            // 设置field内容

            // field.content = layedit.getContent(id);

            if (field.pushFlag === "on") {
                field.pushFlag = "1";
            } else {
                field.pushFlag = "0";
            }

            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                url: '/recommend/add'
                , type: 'post'
                , data: JSON.stringify(field)
                , contentType: 'application/json'
                , success: function (res) {
                    if (res.code === 200) {
                        layer.msg(res.data, {
                            time: 1000
                        });
                    }
                }
            });
            parent.layui.table.reload('LAY-app-content-list'); //重载表格
            // 测试弹出信息
            parent.layer.msg('添加成功');
            parent.layer.close(index); //再执行关闭
        });
    })
</script>

</body>
</html>